
function urlChange(commonUrl, url) {
  return commonUrl + url;
}
function params(obj) {
	let result = '';
	let item;
	for (item in obj) {
		if (obj[item] && String(obj[item])) {
			result += `&${item}=${obj[item]}`;
		}
	}
	return result;
}
 // let commonUrl = "https://www.redifinetd.com/enforce_remote" //线上版
let commonUrl = "https://enforce.redifinetd.com/enforce_remote" //线上版
// let commonUrl = "http://192.168.0.110:8103" //开发版

const handlerPost = (
  url,
  data,
  type = "POST",
  IsLoad = true,
  IsErrorTip = true
) => {
  let promise = new Promise((resolve, reject) => {
    let success = false;
    if (IsLoad) {
      setTimeout(() => {
        if (!success) {
          uni.showLoading({
            title: "加载中..."
          });
        }
        setTimeout(() => {
          if (!success) {
            uni.hideLoading();
          }
        }, 2000);
      }, 400);
    }

	let URL;
	if( type == "GET") {
		URL = `${commonUrl}${url}`
	}else if(type = "POST") {
		URL = urlChange(commonUrl, url)
		data = {
		  ...data
		};
	}
    uni.request({
      url: URL,
      data,
      method: type,
      header: {
        "content-type": "application/json;charset=UTF-8"
      },
      success: res => {
		if (res.data.success) {
		  success = true;
		  uni.hideLoading();
		} else {
		  success = true;
		  if (IsErrorTip) {
			uni.hideLoading();
			if (res.data.code !== "S000") {
				uni.showToast({
				  title: res.data.msg,
				  icon: "none",
				  duration: 1500
				});
			}
		  }
		}

        resolve(res.data);
      },
      error: function (e) {
        reject(e);
      }
    });
  });
  return promise;
};

export default {
	handlerPost,
	commonUrl
};
